package org.moonlight.jvm.classfile.constant.info.member;

import org.moonlight.jvm.classfile.ClassReader;
import org.moonlight.jvm.classfile.constant.StaticConstantPool;
import org.moonlight.jvm.classfile.constant.ConstantInfo;

import java.util.Map;

/**
 * CONSTANT_Fieldref_info表示字段符号引用，
 * CONSTANT_Methodref_info表示普通（非接口）方法符号引用，
 * CONSTANT_InterfaceMethodref_info表示接口方法符号引用。
 * 这三种常量结构一模一样, 结构如下：
 *   CONSTANT_MemberRef_info {
 *       u1  tag
 *       u2  class_index
 *       u2  name_and_type_index
 *   }
 *
 * class_index和name_and_type_index都是常量池索引，分别指向CONSTANT_Class_info和CONSTANT_NameAndType_info常量
 *
 * @author Moonlight
 * @createTime 2023/8/15 14:52
 **/
public abstract class ConstantMemberRefInfo implements ConstantInfo {

    protected StaticConstantPool scp;
    protected int classIdx;
    protected int nameAndTypeIdx;

    public ConstantMemberRefInfo(StaticConstantPool scp) {
        this.scp = scp;
    }

    @Override
    public void readInfo(ClassReader reader) {
        this.classIdx = reader.readUint16();
        this.nameAndTypeIdx = reader.readUint16();
    }

    public String className() {
        return this.scp.getClassName(this.classIdx);
    }

    public Map<String, String> nameAndDescriptor() {
        return this.scp.getNameAndType(this.nameAndTypeIdx);
    }

}
